/**
 * 漆水河 脚手架基础公开课
 * 2022/8/13 --- 2022/12/13
 * 有疑问可加QQ群: 809126489，后续会持续输出与大家一起成长
 */
package com.qishuihe.common.infrastructure.security.filter;

import com.alibaba.fastjson.JSONObject;
import com.qishuihe.basedomain.infrastructure.vo.LoginUserVO;
import com.qishuihe.basedomain.infrastructure.vo.UserVO;
import com.qishuihe.baseinterface.constant.CommonConstant;
import com.qishuihe.baseinterface.utils.JWTUtil;
import com.qishuihe.baseinterface.utils.StringUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class JWTSecurityFilter extends OncePerRequestFilter {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            checkToken(request,response,filterChain);
    }

    private void checkToken(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        String token = request.getHeader(CommonConstant.TOKEN_STR);
        if(!StringUtil.isEmpty(token)){
            Claims claims = JWTUtil.parseJWT(token);
            String username =claims.getSubject();

            //去缓存中用户权限点信息
            //一下是临时代码，等缓存加上后会优化掉
            LoginUserVO loginUserVO = new LoginUserVO();
            String  src = stringRedisTemplate.opsForValue().get(username);

            loginUserVO = JSONObject.parseObject(src, LoginUserVO.class);
            String url =request.getRequestURI();
            if(null==loginUserVO){
                filterChain.doFilter(request,response);
                return;
            }
            if(null==loginUserVO.getPermissions()){
                List<String> permission = new ArrayList<>();
                permission.add(CommonConstant.TOKEN_STR);
                loginUserVO.setPermissions(permission);
                UserVO userVO = new UserVO();
                userVO.setUsername(username);
                loginUserVO.setUserVO(userVO);
            }
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(loginUserVO,null, loginUserVO.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
        }
        filterChain.doFilter(request,response);
    }


}
